package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PictureSortMapper;
import com.itheima.pojo.PictureSort;
import com.itheima.pojo.Todo;
import com.itheima.service.PictureSortService;
import com.itheima.utils.StringUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author hrz
 * @version 1.0
 * @date 2020-08-04 19:12
 */
@Service
public class PictureSortServiceImpl implements PictureSortService {
    @Resource
    private PictureSortMapper pictureSortMapper;
    @Override
    public String add(PictureSort pictureSort) {
        int insert = pictureSortMapper.insert(pictureSort);
        if (insert == 0) {
            return "failed";
        }
        return "succeed";
    }

    @Override
    public String delete(PictureSort pictureSort) {
        pictureSort.setStatus(false);
        int update = pictureSortMapper.updateByPrimaryKeySelective(pictureSort);
        if (update == 0) {
            return "failed";
        }
        return "succeed";
    }

    @Override
    public String eidt(PictureSort pictureSort) {
        // 先查出目前排序数字最大的实体类
        Integer maxSort = pictureSortMapper.getMaxSort();
        // 设置排序值为最大值加一
        pictureSort.setSort(maxSort + 1);
        int update = pictureSortMapper.updateByPrimaryKeySelective(pictureSort);
        if (update == 0) {
            return "failed";
        }
        return "succeed";
    }
    
    @Override
    public PictureSort findPictureSortByUid(String uid) {
        PictureSort pictureSort = pictureSortMapper.selectByPrimaryKey(uid);
        return pictureSort;
    }

    @Override
    public Page<PictureSort> getPageList(Map<String, Object> searchMap) {
        Example example = new Example(PictureSort.class);
        Example.Criteria criteria = example.createCriteria();
        Integer currentPage = (Integer) searchMap.get("currentPage");
        if (currentPage == null) {
            currentPage = 1;
        }
        Integer pageSize = (Integer) searchMap.get("pageSize");
        if (pageSize == null) {
            pageSize = 10;
        }
        criteria.andEqualTo("status",1);
        example.setOrderByClause("`sort` DESC");
        PageHelper.startPage(currentPage, pageSize);
        Page<PictureSort> pageList = (Page<PictureSort>) pictureSortMapper.selectByExample(example);
        return pageList;
    }
}
